package com.groot.common.controller;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @decoration medical 站点servlet控制分发器
 * classpath：xin.spring.medical.common.controller.BaseServletController.java
 * @author spring
 * @date 2019-02-26 17:17:00
 */
public class BaseServletController extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	/**
	 * Log4j
	 */
	private static Logger LOGGER = LoggerFactory.getLogger(BaseServletController.class);
	
	/**
	 * medical站点统一分发请求控制方法
	 */
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LOGGER.info("客户端请求地址：" + request.getRemoteAddr());
		String method = request.getParameter("method");
		if(method == null || method.isEmpty()){
			method = "execute";
		}
		
		LOGGER.info("客户端请求方法：" + method);
		
		Class<? extends BaseServletController> c = this.getClass();
		System.out.println("c是什么:"+c);
		try {
			Method m = c.getMethod(method, HttpServletRequest.class, HttpServletResponse.class);
			System.out.println("m是什么:"+m);
			String result = (String) m.invoke(this, request, response);
			System.out.println("我是谁"+result);
			
			if(result != null && !result.isEmpty()){
				request.getRequestDispatcher(result).forward(request, response);
				/*response.sendRedirect(result);*/
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
